package dao;

import model.ClassInfo;
import util.DatabaseUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

// 插入新的班级信息
public class ClassDAO {
    public static void addClass(ClassInfo classInfo) {
        String sql = "INSERT INTO class_info (class_name, grade, teacher, student_count, description) VALUES (?, ?, ?, ?, ?)";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            
            pstmt.setString(1, classInfo.getClassName());
            pstmt.setString(2, classInfo.getGrade());
            pstmt.setString(3, classInfo.getTeacher());
            pstmt.setInt(4, classInfo.getStudentCount());
            pstmt.setString(5, classInfo.getDescription());
            
            pstmt.executeUpdate();
            
            ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                classInfo.setId(rs.getInt(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static List<ClassInfo> getAllClasses() {
        List<ClassInfo> classes = new ArrayList<>();
        String sql = "SELECT * FROM class_info";
        
        try (Connection conn = DatabaseUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                classes.add(new ClassInfo(
                    rs.getInt("id"),
                    rs.getString("class_name"),
                    rs.getString("grade"),
                    rs.getString("teacher"),
                    rs.getInt("student_count"),
                    rs.getString("description")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return classes;
    }

    public static void updateClass(ClassInfo classInfo) {
        String sql = "UPDATE class_info SET class_name=?, grade=?, teacher=?, student_count=?, description=? WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, classInfo.getClassName());
            pstmt.setString(2, classInfo.getGrade());
            pstmt.setString(3, classInfo.getTeacher());
            pstmt.setInt(4, classInfo.getStudentCount());
            pstmt.setString(5, classInfo.getDescription());
            pstmt.setInt(6, classInfo.getId());
            
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void deleteClass(int id) {
        String sql = "DELETE FROM class_info WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static ClassInfo getClassById(int id) {
        String sql = "SELECT * FROM class_info WHERE id=?";
        
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                return new ClassInfo(
                    rs.getInt("id"),
                    rs.getString("class_name"),
                    rs.getString("grade"),
                    rs.getString("teacher"),
                    rs.getInt("student_count"),
                    rs.getString("description")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
//对数据库中的班级数据的操作，如获取所有班级、根据ID获取班级、新增班级、删除班级和更新班级信息。